package edu.scdx.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.scdx.common.util.JsonUtil;
import edu.scdx.entity.Goods;
import edu.scdx.entity.User;
import edu.scdx.service.UserInfoService;
import edu.scdx.service.UserService;
import edu.scdx.service.GoodsService;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    
    @Autowired
    private UserInfoService userInfoService;
    
    @Autowired
    private GoodsService goodsService;
    
    public GoodsService getGoodsService() {
		return goodsService;
	}



	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}



	public UserInfoService getUserInfoService() {
		return userInfoService;
	}



	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}



	public UserService getUserService() {
		return userService;
	}



	public void setUserService(UserService userService) {
		this.userService = userService;
	}



	@RequestMapping("/logIn.do")
    public String login(Model model,HttpSession session,String userTel,String userPassword){
		
		if(userTel.equals("admin") && userPassword.equals("admin")){
			return "admin";
		}
    	
		if(userTel.trim().equals("") || userPassword.trim().equals("")){
			return "telNull";
		}
		
    	User user=userService.checkLogin(userTel,userPassword);
    	
    	if(user!=null){
    		
    		session.setAttribute("userTel", userTel);
    		
    		List<Goods> goods = goodsService.getAll();
    		
    		model.addAttribute("list",goods);
    		
    		
    		
    		return "welcome";
    	}
    	
    	return "notHaveUser";
    	
    }
	
	@RequestMapping("/reg.do")
    public String reg(Model model,HttpSession session,String userTel,String userName,String userPassword,String userPassword1){
    	
		
		if(userTel.trim().equals("") || userPassword.trim().equals("")){
			return "telNull";
		}
		
		if(userTel.trim().length()!=11){
			return "telBit";
		}
		
		if(userPassword.trim().length()<4 || userPassword.trim().length()>8 ){
			return "passBit";
		}
		
		if(!userPassword.equals(userPassword1)){
			return "passNotSame";
		}
		
		
		User user1=userService.checkLogin1(userTel);
		
		if(user1!=null){
    		
    		return "alreadyhave";
    	}
		
		else{
			
			User user2=new User(userTel,userPassword,userName);
			
			userService.addUser(user2);
			
			userInfoService.addInfo(userTel,userName);
		      
		    return "resSucceed";
		}
		
		
    	
    }
	
	

}
